/*
 * @Descripttion: 获取选择的地点信息
 * @msg1: 出口方法 getLocation()
 * @version: 
 * @Author: yuanliutech
 * @Date: 2025-03-12 16:56:41
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-18 15:00:36
 */
const app = getApp()
import {
  showMsg
} from '../../../utils/config'
const chooseLocation = requirePlugin('chooseLocation');
module.exports = Behavior({
  data: {},
  lifetimes: {
    created() {
      console.log('create');
    },
    attached() {
      console.log('attached');
    },
    onHide() {
      console.log('onHide');
    },
    detached() {
      console.log('detached');
      chooseLocation.setLocation(null);
    }
  },
  pageLifetimes: {
    show() {
      this.setLocation()
    },
    hide() {
      // 页面卸载时设置插件选点数据为null，防止再次进入页面，geLocation返回的是上次选点结果
      console.log("地点页面隐藏");
      chooseLocation.setLocation(null);
    }

  },
  methods: {
    goLocation() {
      wx.showLoading({
        title: '加载中',
        mask: true,
      })
      wx.getLocation({
        type: 'gcj02',
        isHighAccuracy: false, //开启高精度定位
        success(res) {
          console.log(res);
          wx.hideLoading()
          const key = '44FBZ-OVKKP-T23D5-VGHUE-XDXC3-BDF53'; //使用在腾讯位置服务申请的key
          const referer = '精卫安'; //调用插件的app的名称
          const location = JSON.stringify({
            latitude: res.latitude,
            longitude: res.longitude
          });
          const category = '生活服务,娱乐休闲，公司企业';
          wx.navigateTo({
            url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
          });
        },
        fail(err) {
          console.log(err);
          wx.showToast({
            title: '定位失败,请在微信小程序设置中>位置信息>打开定位功能',
            icon: 'none',
            duration: 3000
          })
        }
      })
    },
    setLocation() {
      const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
      console.log(location);
      if (location !== null) {
        console.log("发送地点");
        this.getLocation(location)
      }
    }
  }
})